package org.spring.springboot.app.adv.advexam.service.impl;

import com.sun.org.apache.regexp.internal.RE;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.app.adv.advcombinfo.domain.po.AdvCombInfoPO;
import org.spring.springboot.app.adv.advcombinfo.service.IAdvCombInfoService;
import org.spring.springboot.app.adv.advexam.dao.AdvExamDao;
import org.spring.springboot.app.adv.advexam.domain.po.AdvExamPO;
import org.spring.springboot.app.adv.advexam.domain.vo.AdvExamDealVO;
import org.spring.springboot.app.adv.advexam.domain.vo.AdvExamQueryVO;
import org.spring.springboot.app.adv.advexam.domain.vo.AdvExamSearchVO;
import org.spring.springboot.app.adv.advexam.service.IAdvExamService;
import org.spring.springboot.app.adv.advfamilylockinfo.service.IAdvFamilyLockInfoService;
import org.spring.springboot.app.adv.advinfo.dao.AdvInfoDao;
import org.spring.springboot.app.adv.advinfo.domain.po.AdvInfoPO;
import org.spring.springboot.app.adv.advmobilemaininfo.service.IAdvMobileMainInfoService;
import org.spring.springboot.app.adv.advmobilerightinfo.service.IAdvMobileRightInfoService;
import org.spring.springboot.app.adv.advscreeninfo.dao.AdvScreenInfoDao;
import org.spring.springboot.app.adv.advscreeninfo.domain.po.AdvScreenInfoPO;
import org.spring.springboot.app.adv.advscreeninfo.service.IAdvScreenInfoService;
import org.spring.springboot.app.base.Constants;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.User;
import org.spring.springboot.app.sys.office.domain.po.SysOfficeWalletPO;
import org.spring.springboot.app.sys.office.service.ISysOfficeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class AdvExamServiceImpl implements IAdvExamService{

    private static final Logger LOGGER = LoggerFactory.getLogger(AdvExamServiceImpl.class);


    @Autowired
    private AdvExamDao advExamDao;
    @Autowired
    private AdvInfoDao advInfoDao;
    @Autowired
    private IAdvScreenInfoService advScreenInfoService;
    @Autowired
    private IAdvFamilyLockInfoService advFamilyLockInfoService;
    @Autowired
    private IAdvMobileMainInfoService advMobileMainInfoService;
    @Autowired
    private IAdvMobileRightInfoService advMobileRightInfoService;


    @Override
    public AdvExamPO find(String id) {
        return advExamDao.selectByPrimaryKey(id);
    }

    @Override
    public int save(AdvExamPO po) {
        po.preInsert();
        return advExamDao.insertSelective(po);
    }

    @Override
    public int update(AdvExamPO po) {
        po.preUpdate();
        return advExamDao.updateByPrimaryKeySelective(po);
    }

    @Override
    public int delete(String id) {
        AdvExamPO po = new AdvExamPO();
        po.setId(id);
        po.preDelete();
        return advExamDao.updateByPrimaryKeySelective(po);
    }

    @Override
    public List<AdvExamQueryVO> findPageWithAdv(AdvExamSearchVO searchVO, int pageNum, int pageSize) {
        return advExamDao.findPageWithAdv(searchVO,pageNum,pageSize);
    }

    @Override
    public AdvExamQueryVO findWithAdv(String id) {
        return advExamDao.findWithAdv(id);
    }

    @Override
    public R deal(AdvExamDealVO vo) {
        R r = new R();
        AdvExamQueryVO advExamQueryVO = advExamDao.findWithAdv(vo.getId());
        if (advExamQueryVO.getState().intValue() != Constants.ADV_EXAM_STATE_1) {
            return r.fail().setMessage("不是待审核状态，不能操作");
        }

        if (vo.getState().intValue() == Constants.ADV_EXAM_STATE_2) {//通过
            //根据活动类型做不同的处理
            if(advExamQueryVO.getType().intValue() == Constants.ADV_INFO_TYPE_1){
                agreeAdvMobileMainInfo(advExamQueryVO.getAdvId());
            }else if(advExamQueryVO.getType().intValue() == Constants.ADV_INFO_TYPE_2){
                agreeAdvMobileRightInfo(advExamQueryVO.getAdvId());
            }else if(advExamQueryVO.getType().intValue() == Constants.ADV_INFO_TYPE_3){
                agreeAdvScreenInfo(advExamQueryVO.getAdvId());
            }else if(advExamQueryVO.getType().intValue() == Constants.ADV_INFO_TYPE_4){
                agreeAdvFamilyLockInfo(advExamQueryVO.getAdvId());
            }

        } else if (vo.getState().intValue() == Constants.ADV_EXAM_STATE_3) {//拒绝
            //根据活动类型做不同的处理
            if(advExamQueryVO.getType().intValue() == Constants.ADV_INFO_TYPE_1){

            }else if(advExamQueryVO.getType().intValue() == Constants.ADV_INFO_TYPE_2){

            }else if(advExamQueryVO.getType().intValue() == Constants.ADV_INFO_TYPE_3){

            }else if(advExamQueryVO.getType().intValue() == Constants.ADV_INFO_TYPE_4){

            }

        } else {
            return r.fail().setMessage("请传入正确的状态");
        }
        //更新审核状态
        AdvExamPO advExamPO = new AdvExamPO();
        advExamPO.setId(vo.getId());
        advExamPO.setState(vo.getState());
        advExamPO.setRemarks(vo.getRemarks());
        advExamPO.preUpdate();
        if (advExamDao.updateByPrimaryKeySelective(advExamPO) < 1) {
            throw new RuntimeException("更新审核表失败");
        }
        return r.success();
    }

    @Override
    public int addExam(String officeId, String id) {
        AdvExamPO examPO = new AdvExamPO();
        examPO.setOfficeId(officeId);
        examPO.setAdvId(id);
        examPO.setState(Constants.ADV_EXAM_STATE_1);
        examPO.preInsert();
        if (advExamDao.insertSelective(examPO) < 1) {
            throw new RuntimeException("添加审核失败");
        }
        return 1;
    }

    /**
     * 手机屏保广告处理成功
     * @param advId
     */
    private void agreeAdvMobileMainInfo(String advId) {

        //分钱
        advMobileMainInfoService.profitDivision(advId);
    }

    /**
     * 手机右侧分类广告处理成功
     * @param advId
     */
    private void agreeAdvMobileRightInfo(String advId) {
    }

    /**
     * 广告机广告处理成功
     * @param advId
     */
    private void agreeAdvScreenInfo(String advId) {
        AdvInfoPO mainPO = advInfoDao.selectByPrimaryKey(advId);
        AdvScreenInfoPO po = advScreenInfoService.find(advId);
        boolean profitDicvision = false;
        if (po.getState().intValue() == Constants.ADV_SCREEN_INFO_STATE_0) {//临时
            AdvScreenInfoPO _po = new AdvScreenInfoPO();
            _po.setId(advId);
            if(mainPO.getOfficeId().equals(po.getBuyOfficeId())){//自己发布自己的套餐
                _po.setState(Constants.ADV_SCREEN_INFO_STATE_2);//直接同意
                profitDicvision = true;
            }else{
                _po.setState(Constants.ADV_SCREEN_INFO_STATE_1);//待接受
            }
            if (advScreenInfoService.update(_po) < 1) {
                throw new RuntimeException("修改广告机广告失败");
            }
            if (profitDicvision) {
                //分钱
                advScreenInfoService.profitDivision(advId);
            }
        }
    }

    /**
     * 家庭锁广告处理成功
     * @param advId
     */
    private void agreeAdvFamilyLockInfo(String advId) {


        //分钱
        advFamilyLockInfoService.profitDivision(advId);
    }

}
